var info = $('.info');
var info_list = $('tbody');
var submit = $('.sendInfo input');
// 管理员添加任务
$('.btn').click(function () {
    if(submit[1].value==''){
        submit[1].value=submit[0].value;
    }
    console.log(submit[0].value,submit[1].value,submit[2].value)
$.ajax({
        type: 'post',
        // url: './api/task/masterAddToTaskList',
        url: './api/task/masterAddToTaskList',
        data:{
            task:{
                username:submit[0].value,
                schoolaccount:submit[0].value,
                schoolpassword:submit[1].value,
                projectID:submit[2].value,
                status:'等待中'
            }
        },
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", localStorage.getItem("token"));
        },
        success: function (data) {
            alert('提交成功');
            // 刷新页面
            window.location.reload();
        },
        error: function (err) {
            console.log(err);
        }
    })
});
(function () {
    // var btn = $('.button');

    // 判断是否存在token
    if (localStorage.getItem("token")) {
        // jquery ajax请求 验证token
        $.ajax({
            type: 'post',
            url: './api/user/verify',
            beforeSend: function (request) {
                request.setRequestHeader("Authorization", localStorage.getItem("token"));
            },
            success: function (data) {
                info.append(`
                <div>欢迎用户: ${data.username}</div>`
                );
            },
            error: function (err) {
                // 重定向到首页
                window.location.href = '/';
            }
        })
    } else {
        
        
    }
    getTaskList();
})();
// 获取任务列表
function getTaskList() {
    $.ajax({
        type: 'post',
        url: './api/task/getAllTaskList',
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", localStorage.getItem("token"));
        },
        success: function (data) {


            console.log(data.data[1]);
            var line = data.data.length
            for (var i = 0; i < line; i++) {
                info_list.append(`
                <tr>
                <td>${data.data[line - i - 1].username}</td>
                <td>${data.data[line - i - 1].projectID}</td>
                <td>${data.data[line - i - 1].createdAt}</td>
                <td>${data.data[line - i - 1].status}</td>
                </tr>`
                );
                console.log(line - i);
            };
        },
        error: function (err) {
            console.log(err);
        }
    })
};
